/**
 * 质控病种列表初始化
 */
var SdsQcentity = {
    id: "entityTree",	//表格id
    seItem: null,		//选中的条目
    table: null,
    ztree: null,
    layerIndex: -1,
    entityId: 0,
    entityDesc: null
};
/**
 * 病种点击事件
 */
SdsQcentity.onClickEntity = function (e, treeId, treeNode) {
    if (treeNode.pId == null || treeNode.pId == 0) {
        Sys.info("不能选择根节点（" + treeNode.name + "），请重新选择具体病种。");
        return false;
    }
    SdsQcentity.entityId = treeNode.id;
    SdsQcentity.entityDesc = treeNode.name;
    //查询质控项目
    SdsQcformItem.seItem = null;
    SdsQcformItem.search(1);
};

/**
 * 表单项目表管理初始化
 */
var SdsQcformItem = {
    id: "SdsQcformItemTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
SdsQcformItem.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '序号', field: 'btIndNo', width: 50, align: 'center', valign: 'middle', sortable: true},
        {title: '病种字典', field: 'entityId', align: 'center', valign: 'middle', sortable: true, visible: false},
        {title: '代码', field: 'btCode', width: 150, align: 'left', valign: 'middle', sortable: true},
        {title: '描述', field: 'btDesc', width: 300, align: 'left', valign: 'middle', sortable: true},
        {
            title: '上传类型',
            field: 'btUpTypeDict.description',
            width: 100,
            align: 'center',
            valign: 'middle',
            sortable: true
        },
        {title: '数据源', field: 'isConfig', width: 100, align: 'center', valign: 'middle', sortable: true,
            formatter: function (value, row, index) {
                if (value ==0) {
                    return " ";
                } else {
                    return "已配置";
                }
            }},
        {title: '项目类型', field: 'btTypeDict.description', width: 100, align: 'center', valign: 'middle', sortable: true},
        {title: '项目大类', field: 'btItemCat', width: 150, align: 'left', valign: 'middle', sortable: true},
        {title: '项目子类', field: 'btItemSubCat', width: 150, align: 'left', valign: 'middle', sortable: true},
        {
            title: '是否必填',
            field: 'btIsNeeded',
            width: 80,
            align: 'center',
            valign: 'middle',
            sortable: true,
            formatter: function (value, row, index) {
                if (value == 1) {
                    return "<span class='label label-success'>是</span>";
                } else {
                    return "<span class='label label-warning'>否</span>";
                }
            }
        },
        {
            title: '是否有效',
            field: 'btIsActive',
            width: 80,
            align: 'center',
            valign: 'middle',
            sortable: true,
            formatter: function (value, row, index) {
                if (value == 1) {
                    return "<span class='label label-success'>是</span>";
                } else {
                    return "<span class='label label-warning'>否</span>";
                }
            }
        },
        {title: '前置条件', field: 'triggerExp', width: 200, align: 'left', valign: 'middle', sortable: true},
        /*{
            title: '质控时机',
            field: 'checkTimDict.description',
            width: 100,
            align: 'center',
            valign: 'middle',
            sortable: true
        },
        {title: '质控提示', field: 'btTips', width: 200, align: 'center', valign: 'middle', sortable: true},*/
        {title: '备注信息', field: 'btResume', width: 200, align: 'center', valign: 'middle', sortable: true}];
};

/**
 * 检查是否选中
 */
SdsQcformItem.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Sys.info("请先选中表格中的某一记录！");
        return false;
    } else {
        SdsQcformItem.seItem = selected[0];
        return true;
    }
};

/**
 * 点击新增表单项目表
 */
SdsQcformItem.add = function () {
    if (SdsQcentity.entityId) {
        var index = Sys.open('项目维护', Sys.backendPath + '/sds/sdsqcformitem/to_add', ['800px', '520px'], function (layero, index, iframeWin) {
            $(iframeWin.document).find("#entityId").attr("value", SdsQcentity.entityId);
        });
        this.layerIndex = index;
    } else {
        Sys.info("请先选中某一病种！");
    }

};

/**
 * 打开查看表单项目表详情
 */
SdsQcformItem.edit = function (r) {
    var flag = false;
    var id = 0;
    if (r) {
        id = r.id;
        flag = true;
    } else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
    if (flag) {
        var index = Sys.open('表单项目表详情', Sys.backendPath + '/sds/sdsqcformitem/to_update/' + id, ['800px', '520px']);
        this.layerIndex = index;
    }
};

/**
 * 作废表单项目表
 */
SdsQcformItem.del = function (id, name) {
    var flag = false;
    if (id) {
        flag = true;
    } else if (this.check()) {
        id = this.seItem.id;
        name = this.seItem.name
        flag = true;
    }
    if (flag) {
        var operation = function () {
            var ajax = new $ax(Sys.backendPath + "/sds/sdsqcformitem/setInvalidStatus", function (data) {
                Sys.success("作废成功!");
                // SdsQcformItem.table.refresh();
                SdsQcformItem.search();
            }, function (data) {
                Sys.error("作废失败!" + data.responseJSON.message + "!");
            });
            ajax.set("sdsQcformItemId", id);
            ajax.start();
        };
        Sys.confirm("是否作废该表单项目?", operation);
    }
};

/**
 * 生成表单静态模板
 */
SdsQcformItem.creatFormHtmlField = function () {
    if (SdsQcentity.entityId) {
        var operation = function () {
            var ajax = new $ax(Sys.backendPath + "/sds/sdsqcformitem/creatFormHtmlField", function (rd) {
                $("#tips").text("");
                var showForm = function () {
                    var index = layer.open({
                        type: 2,
                        title: '病种名称：' + SdsQcentity.entityDesc,
                        area: ['1000px', '500px'], //宽高
                        fix: false, //不固定
                        maxmin: true,
                        content: Sys.backendPath + "/sds/sdsqcformitem/to_form/" + rd
                    });
                    this.layerIndex = index;
                }
                Sys.confirm("查看表单模板?", showForm);
            }, function (data) {
                Sys.error("更新失败!" + data.responseJSON.message + "!");
            });
            ajax.set("entityId", SdsQcentity.entityId);
            ajax.start();
        };
        Sys.confirm("确认更新表单模板?", operation);
    } else {
        Sys.info("请先选中某一病种！");
    }
};

/**
 * 重新生成全部单静态模板
 */
SdsQcformItem.creatAllFormHtmlField = function () {
    $.messager.progress({title: "提示", msg: '正在生成表单模板', text: '生成中....'});
    var ajax = new $ax(Sys.backendPath + "/sds/sdsqcformitem/creatAllFormHtmlField", function (rd) {
            $.messager.progress("close");
            $("#tips").text("");
            $.messager.alert("提示", "生成成功", 'success');
        }, function (data) {
            $.messager.progress("close");
            $.messager.alert("错误", "生成失败", 'error');
        }
    );
    ajax.setAsync(true);
    ajax.start();
};

/**
 * 查询表单项目表列表
 */
SdsQcformItem.search = function (pageNum) {
    var queryData = {};
    queryData['entityId'] = SdsQcentity.entityId;
    SdsQcformItem.table.refresh({query: queryData, pageNumber: pageNum});
    //查询表单有无更新
    var ajax = new $ax(Sys.backendPath + "/sds/sdsqcformitem/getFormUpdateSta", function (rd) {
        if (rd != 0) {
            $("#tips").text("该病种项目有变更，请重新生成填报模板！");
        } else {
            $("#tips").text("");
        }
    });
    ajax.set("entityId", SdsQcentity.entityId);
    ajax.start();
};

SdsQcformItem.exp = function () {
    if (SdsQcentity.entityId) {
        var operation = function () {
            Sys.download(Sys.backendPath + "/sds/sdsqcformitem/export", {entityId: SdsQcentity.entityId});
        };
        Sys.confirm("是否导出到excel", operation);
    } else {
        Sys.info("请先选中某一病种！");
    }
};

SdsQcformItem.imp = function () {
    if (SdsQcentity.entityId) {
        var index = Sys.open('导入项目信息', Sys.backendPath + '/sds/sdsqcformitem/to_import', ['550px', '300px'], function (layero, index, iframeWin) {
            $(iframeWin.document).find("#entityId").attr("value", SdsQcentity.entityId);
        });
        this.layerIndex = index;
    } else {
        Sys.info("请先选中某一病种！");
    }
};

SdsQcformItem.template = function () {
    window.location.href = Sys.backendPath + "/static/backend/sds/sdsqcformitem/tmp/XXXX_Template.xlsx";
};

SdsQcformItem.syncDataConfig = function () {
    if (SdsQcentity.entityId) {
        var ajax = new $ax(Sys.backendPath + "/sds/sdsqcformitem/syncDataConfig", function (data) {
            $.messager.alert("同步成功", data.message, 'success');
        }, function (data) {
            $.messager.alert("同步失败", data.message, 'error');
        });
        ajax.set("entityId", SdsQcentity.entityId);
        ajax.start();
    } else {
        Sys.info("请先选中某一病种！");
    }
};

/**
 * 同步全部病种通用项目数据源
 */
SdsQcformItem.syncAllDataConfig = function () {
    $.messager.progress({title: "提示", msg: '正在同步全部病种通用项目数据源', text: '同步中....'});
    var ajax = new $ax(Sys.backendPath + "/sds/sdsqcformitem/syncAllDataConfig", function (data) {
        $.messager.progress("close");
        $.messager.alert("同步成功", '<div style="height:200px;overflow-y:scroll">' +data.message+ '</div>', '');
    }, function (data) {
        $.messager.progress("close");
        $.messager.alert("同步失败", data.message, 'error');
    });
    ajax.setAsync(true);
    ajax.start();
};

$(function () {
    //查询病种树
    var ztree = new $ZTree("entityTree", Sys.backendPath + "/sds/sdsqcentity/treebycode/Up");
    ztree.bindOnClick(SdsQcentity.onClickEntity);
    ztree.init();
    SdsQcentity.ztree = ztree;
    fuzzySearch('entityTree', '#seachKey', null, true);

    var formColumns = SdsQcformItem.initColumn();
    var formTable = new BSTable(SdsQcformItem.id, Sys.backendPath + "/sds/sdsqcformitem/list", formColumns);
    formTable.setPaginationType("server");
    formTable.setPageSize(15);
    formTable.setSubHeight(-10);
    formTable.setShowExport(false);
    formTable.setShowRefresh(false);
    formTable.setShowToggle(false);
    formTable.setShowColumns(false);
    formTable.setQueryParams({"entityId": 0})
    formTable.setOnDblClickRow(SdsQcformItem.edit); //双击数据行打开表单填报
    SdsQcformItem.table = formTable.init();
});
